<template>
  <h1>循环指令</h1>
  <ul>
    <!-- item从1开始,每执行一次,item的值+1,一直到n-->
    <!-- v-for的效果:执行6次,生成6个li   -->
    <li v-for="item in 6">{{item}}</li>
  </ul>

  <ul>
    <!-- 遍历数组，item就是当前遍历到的数组元素   -->
    <li v-for="item in productArr">{{item}}</li>
  </ul>
  <table border="1px">
    <tr>
      <th>汽车编号</th>
      <th>汽车品牌</th>
      <th>汽车价格</th>
      <th>汽车库存</th>
      <th>操作</th>
    </tr>
    <!-- 循环几次，就生成几个tr   -->
    <!-- index从0开始 -->
    <tr v-for="(item,index) in productArr">
      <td>{{index+1}}</td>
      <td>{{item.title}}</td>
      <td>{{item.price}}</td>
      <td>{{item.num}}</td>
      <td><button @click="del(index)">删除</button></td>
    </tr>
  </table>
</template>

<script setup>
import {ref} from "vue";

const productArr = ref([
  {title:'奔驰',price:30,num:200},
  {title:'宝马',price:40,num:300},
  {title:'宾利',price:60,num:1200},
  {title:'su7',price:20,num:600}
]);

const del = (i)=>{
  if(confirm('确定删除吗？')){
    //从下标i开始,删除1个元素
    productArr.value.splice(i,1);
  }
}
</script>

<style scoped>

</style>